﻿// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED!
// YOU MAY USE THIS CODE: HOWEVER THIS GRANTS NO FUTURE RIGHTS.
// see https://spsearchbestbetsmigration.codeplex.com/license and https://spsearchbestbetsmigration.codeplex.com/ for further details and license information

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace SPSearchBestBetsMigration
{
    internal class SearchKeyword
    {
        private const string termXPath = "Term";
        private const string synonymsXPath = "Synonyms/Synonym";
        private const string definitionXPath = "Definition";
        private const string contactXPath = "Contact";
        private const string startDtXPath = "StartDate";
        private const string endDtXPath = "EndDate";
        private const string reviewDtXPath = "ReviewDate";
        private const string bestBetsXPath = "BestBets/BestBet";

        public string Term { get; set; }
        public string Contact { get; set; }
        public string Definition { get; set; }
        public DateTime StartDate { get; set; }
        public string EndDate { get; set; }
        public string ReviewDate { get; set; }
        public List<string> Synonyms { get; set; }
        public List<SearchBestBets> BestBets { get; set; }

        public SearchKeyword(XPathNavigator nav)
        {
            Init();

            #region Get Term
            XPathNavigator termNode = nav.SelectSingleNode(termXPath);
            if (!string.IsNullOrEmpty(termNode.Value))
            {
                this.Term = termNode.Value;
            }
            #endregion

            #region Get Contact
            XPathNavigator ctNode = nav.SelectSingleNode(contactXPath);
            if (!string.IsNullOrEmpty(ctNode.Value))
            {
                this.Contact = ctNode.Value;
            }
            #endregion

            #region Get Definition
            XPathNavigator defNode = nav.SelectSingleNode(definitionXPath);
            if (!string.IsNullOrEmpty(defNode.Value))
            {
                this.Definition = defNode.Value;
            }
            #endregion

            #region Get StartDate
            XPathNavigator startNode = nav.SelectSingleNode(startDtXPath);
            if (!string.IsNullOrEmpty(startNode.Value))
            {
                DateTime dt;
                if (DateTime.TryParse(startNode.Value,out dt)) {
                    this.StartDate = dt;
                }
            }
            #endregion

            #region Get EndDate
            XPathNavigator endNode = nav.SelectSingleNode(endDtXPath);
            if (!string.IsNullOrEmpty(endNode.Value))
            {
                this.EndDate = endNode.Value;
            }
            #endregion

            #region Get ReviewDate
            XPathNavigator revNode = nav.SelectSingleNode(reviewDtXPath);
            if (!string.IsNullOrEmpty(revNode.Value))
            {
                this.ReviewDate = revNode.Value;
            }
            #endregion

            #region Get Synonyms
            XPathNodeIterator symsIterator = nav.Select(synonymsXPath);
            this.Synonyms.Clear();
            while (symsIterator.MoveNext())
            {
                XPathNavigator sym = symsIterator.Current;
                this.Synonyms.Add(sym.Value);
            }
            #endregion

            #region Get Best Bets
            XPathNodeIterator bbIterator = nav.Select(bestBetsXPath);
            this.BestBets.Clear();
            while (bbIterator.MoveNext())
            {
                XPathNavigator bbet = bbIterator.Current;
                this.BestBets.Add(new SearchBestBets(bbet));
            }

            #endregion

        }

        /// <summary>
        /// Returns the Synonyms as a single string that is semicolon delimited
        /// </summary>
        /// <returns>Synonyms as a single string that is semicolon delimited</returns>
        public string GetSynonymsString() {
            StringBuilder sb = new StringBuilder();
            foreach(string sym in this.Synonyms) {
                sb.Append(sym + ";");
            }
            string output = sb.ToString();
            return output.Substring(0,output.Length-1);
        }

        private void Init() {
            this.Synonyms = new List<string>();
            this.BestBets = new List<SearchBestBets>();
        }
    }

}
